<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Twitter extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'content','clubname','club_id','consumer_id','likes'
    ];

    public function club(){
        return $this->belongsTo(Club::class);
    }
    public function twitterPics(){
        return $this->hasMany(Twitterpic::class,'');
    }
    public function consumer(){
        return $this->belongsTo(Consumer::class);
    }

    public function comments(){
        return $this->hasMany(Comment::class);
    }

    public function replies(){
        return $this->hasManyThrough(Reply::class,Comment::class,'twitter_id','comment_id','id');
    }
    public function keytodata($keyname){
        return collect($keyname)->map(function($key){
            if(  !is_string($this->$key) && !is_int($this->$key)){
                if(  $this->$key  instanceof Carbon){
                    return [$key=>$this->$key->toFormattedDateString()];
                }
                elseif($this->$key instanceof Club){
                    return [$key=>$this->$key['name']];
                }
                elseif($this->$key instanceof Consumer){
                    return [$key=>$this->$key['nickname']];
                }
                else{
                    return [$key=>$this->$key->first()['pic']];

                }

            }

            return [$key=>$this->$key];
        })->collapse()->toArray();

    }
}
